package com.lwc.aop;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lwc.bean.Result;
import com.lwc.exception.BaseException;

import lombok.extern.slf4j.Slf4j;

/**
 * 全局异常处理类，使用@ControllerAdvice 来做，而不是继承ExceptionResolver
 *
 * @author lwc
 */
@ControllerAdvice
@Slf4j
public class ExceptionHandlerAdvice {

	@ResponseBody
	@ExceptionHandler(Exception.class)
	public Object resolveException(Exception ex) {
		log.error(ex.getMessage(), ex);
		return Result.fail();
	}

	@ResponseBody
	@ExceptionHandler(BaseException.class)
	public Object resolveBaseException(BaseException ex) {
		return Result.fail(ex.getResultCode());
	}
}
